<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .preview_wrap {
            width: 400px;
            height: 590px;
        }

        .preview_img {
            position: relative;
            height: 398px;
            border: 1px solid #ccc;
        }

        .mask {
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            width: 300px;
            height: 300px;
            background: #FEDE4F;
            opacity: .5;
            border: 1px solid #ccc;
            cursor: move;
        }

        .big {
            display: none;
            position: absolute;
            left: 410px;
            top: 0;
            width: 500px;
            height: 500px;
            background-color: pink;
            z-index: 999;
            border: 1px solid #ccc;
            overflow: hidden;
        }

        .big img {
            position: absolute;
            top: 0;
            left: 0;
        }
    </style>
    <script>
        // 仿京东放大镜
        window.addEventListener('DOMContentLoaded', function () {
            // 获取元素
            const preview_img = document.querySelector('.preview_img');
            const mask = document.querySelector('.mask');
            const big = document.querySelector('.big');
            const bigImg = document.querySelector('.bigImg');

            // 鼠标移入事件
            preview_img.addEventListener('mouseover', function () {
                mask.style.display = 'block';
                big.style.display = 'block';
            });

            // 鼠标移出事件
            preview_img.addEventListener('mouseout', function () {
                mask.style.display = 'none';
                big.style.display = 'none';
            });

            // 鼠标移动事件
            preview_img.addEventListener('mousemove', function (e) {
                // 盒子坐标
                let x = e.pageX - this.offsetLeft;
                let y = e.pageY - this.offsetTop;
                // 鼠标居中，边界范围
                x -= mask.offsetWidth / 2;
                if (x < 0) x = 0;
                if (x > this.offsetWidth - mask.offsetWidth) x = this.offsetWidth - mask.offsetWidth;
                y -= mask.offsetHeight / 2;
                if (y < 0) y = 0;
                if (y > this.offsetHeight - mask.offsetHeight) y = this.offsetHeight - mask.offsetHeight;
                // 盒子位移
                mask.style.left = `${x}px`;
                mask.style.top = `${y}px`;
                // 右侧大图位移
                let bigX = x * (bigImg.offsetWidth - big.offsetWidth) / (this.offsetWidth - mask.offsetWidth);
                let bigY = y * (bigImg.offsetHeight - big.offsetWidth) / (this.offsetHeight - mask.offsetHeight);
                bigImg.style.left = `-${bigX}px`;
                bigImg.style.top = `-${bigY}px`;
            });
        });
    </script>
</head>

<body>
    <div class="preview_wrap">
        <div class="preview_img">
            <img src="upload/s3.png" alt="">
            <div class="mask"></div>
            <div class="big">
                <img src="upload/big.jpg" alt="" class="bigImg">
            </div>
        </div>
    </div>
</body>

</html>